package com.weijian.util;

import com.weijian.dal.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Session用户工具类
 * 用于获取当前登录用户的信息
 */
public class SessionUserUtil {

    /**
     * 获取当前登录用户
     *
     * @return 当前用户对象，如果未登录返回null
     */
    public static User getCurrentUser() {
        HttpSession session = getSession();
        if (session == null) {
            return null;
        }
        return (User) session.getAttribute("user");
    }

    /**
     * 获取当前登录用户ID
     *
     * @return 用户ID，如果未登录返回null
     */
    public static Long getCurrentUserId() {
        User user = getCurrentUser();
        return user != null ? user.getId() : null;
    }

    /**
     * 获取当前登录用户名
     *
     * @return 用户名，如果未登录返回null
     */
    public static String getCurrentUsername() {
        User user = getCurrentUser();
        return user != null ? user.getUsername() : null;
    }


    /**
     * 设置当前用户信息到Session并设置过期时间
     *
     * @param user       用户对象
     * @param ttlSeconds 过期时间（秒）
     */
    public static void setCurrentUser(User user, long ttlSeconds) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpSession session = attributes.getRequest().getSession(); // 使用无参方法，默认创建Session
            session.setAttribute("user", user);
            session.setAttribute("lastAccessTime", System.currentTimeMillis());
            session.setMaxInactiveInterval((int) ttlSeconds);
        }
    }


    /**
     * 清除当前用户信息
     */
    public static void clearCurrentUser() {
        HttpSession session = getSession();
        if (session != null) {
            session.removeAttribute("user");
            session.removeAttribute("lastAccessTime");
        }
    }

    /**
     * 检查用户是否已登录
     *
     * @return true 如果已登录，否则false
     */
    public static boolean isLoggedIn() {
        return getCurrentUser() != null;
    }

    /**
     * 更新最后访问时间
     */
    public static void updateLastAccessTime() {
        HttpSession session = getSession();
        if (session != null) {
            session.setAttribute("lastAccessTime", System.currentTimeMillis());
        }
    }

    /**
     * 获取Session
     *
     * @return HttpSession对象
     */
    private static HttpSession getSession() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        return attributes.getRequest().getSession(false);
    }

    /**
     * 获取HttpServletRequest
     *
     * @return HttpServletRequest对象
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        return attributes.getRequest();
    }
}